package com.yimi.appstore.fileexplorer;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.util.Log;

import com.yimi.appstore.activity.CategoryFileActivity;
import com.yimi.appstore.fileexplorer.MediaFile.MediaFileType;
import com.yimi.appstore.util.Util;

public class FileCategoryHelper {
	
	private static final String TAG = "FileCategoryHelper";
	private Context mContext ;
    public static final int COLUMN_ID = 0;

    public static final int COLUMN_PATH = 1;

    public static final int COLUMN_SIZE = 2;

    public static final int COLUMN_DATE = 3;
    public enum FileCategory {
         Music, Video, Picture
    }
	
	public FileCategoryHelper( Context cx ){
		this.mContext = cx ;
	}
	
	
    public Cursor query(FileCategory fc) {
        Uri uri = getContentUriByCategory(fc);

        if (uri == null) {
            Log.e(TAG, "invalid uri, category:" + fc);
            return null;
        }

        String[] columns = new String[] {
                FileColumns.ID, FileColumns.DATA, FileColumns.SIZE, FileColumns.DATA_MODIFIED
        };

        return mContext.getContentResolver().query(uri, columns, null, null, null);
    }
	
    
    private Uri getContentUriByCategory(FileCategory fc) {
        Uri uri;
        String volumeName = "external";
        switch(fc) {
            case Music:
                uri = Audio.Media.getContentUri(volumeName);
                break;
            case Video:
                uri = Video.Media.getContentUri(volumeName);
                break;
            case Picture:
                uri = Images.Media.getContentUri(volumeName);
                break;
           default:
               uri = null;
        }
        return uri;
    }
    
    public static FileCategory getCategoryFromPath(String path) {
        MediaFileType type = MediaFile.getFileType(path);
        if (type != null) {
            if (MediaFile.isAudioFileType(type.fileType)) return FileCategory.Music;
            if (MediaFile.isVideoFileType(type.fileType)) return FileCategory.Video;
            if (MediaFile.isImageFileType(type.fileType)) return FileCategory.Picture;
        }
        return FileCategory.Picture;
    }

	
}
